<template>
  <div class="cs-page-container">

    <el-tabs value="second" type="border-card" @tab-click="handleClick">
      <el-tab-pane label="聊天记录" name="first" />
      <el-tab-pane label="视频截图" name="second" />

      <div class="cs-filter-container">
        <el-input
          v-model.trim="listQuery.userId"
          clearable
          placeholder="请输入用户ID"
        />
        <el-select
          v-model="gender"
          placeholder="选择只看..."
          @change="onGenderChange"
        >
          <el-option label="全部" value="全部" />
          <el-option label="女" value="女" />
        </el-select>
        <el-date-picker
          v-model="daterangeValue"
          :picker-options="daterangePickerOptions"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="创建起始时间"
          end-placeholder="创建结束时间"
          value-format="yyyy-MM-dd"
          @change="daterangeChangedTimestamp"
        />
        <el-select
          v-model="listQuery.valid"
          clearable
          placeholder="选择已阅/未阅"
        >
          <el-option label="未阅" value="0" />
          <el-option label="已阅" value="1" />
        </el-select>
        <el-button
          type="primary"
          size="small"
          icon="el-icon-search"
          @click="handleFilter"
        >搜索</el-button>
        <el-button
          type="info"
          size="small"
          icon="el-icon-refresh-right"
          @click="resetFilter"
        >重置</el-button>
      </div>
      <div class="cs-filter-result">
        <el-table
          v-loading="listLoading"
          row-key="chatId"
          :data="list"
          :height="'calc(100vh - 300px)'"
          border
          fit
          size="small"
          highlight-current-row
          element-loading-text="拼命加载中"
          element-loading-spinner="el-icon-loading"
        >
          <el-table-column
            align="center"
            label="频道ID"
          ><template slot-scope="{row}">
            <a target="_blank" :href="row.chatId | filterHref('/anchor/video-thumb-all?id=')">
              查看本次全部截图
            </a>
          </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="用户ID1"
          >
            <template slot-scope="{row}">
              <a target="_blank" :href="row.fromUser | filterHref('/account/account-list?id=')">
                {{ row.fromUser }}
              </a>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="用户1截图"
            width="240"
          >
            <template slot-scope="{row}">
              <image-tooltip :image-id="row.fromUrl">
                <img
                  v-if="row.fromGender > (gender === '全部' ? 0 : 1) && row.fromUrl"
                  slot="img"
                  :src="row.fromUrl+ '?x-oss-process=image/resize,w_150'"
                  style="cursor:pointer;width:100px;"
                  title="点击放大"
                  @click="zoomImage(row.fromUrl)"
                >
              </image-tooltip>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="用户ID2"
          >
            <template slot-scope="{row}">
              <a target="_blank" :href="row.toUser | filterHref('/account/account-list?id=')">
                {{ row.toUser }}
              </a>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="用户2截图"
            width="240"
          >
            <template slot-scope="{row}">
              <image-tooltip :image-id="row.toUrl">
                <img
                  v-if="row.toGender > (gender === '全部' ? 0 : 1) && row.toUrl"
                  slot="img"
                  :src="row.toUrl+ '?x-oss-process=image/resize,w_100'"
                  style="cursor:pointer;width:100px;"
                  title="点击放大"
                  @click="zoomImage(row.toUrl)"
                >
              </image-tooltip>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="更新时间"
          >
            <template slot-scope="{row}">{{ row.updateTime | parseTime }}</template>
          </el-table-column>
          <el-table-column
            align="center"
            label="操作"
            fixed="right"
            width="200"
          >
            <template slot-scope="{ row }">
              <el-button
                v-if="row.valid === 0"
                type="primary"
                size="mini"
                @click="onVerify(row)"
              >已阅</el-button>
              <el-button
                v-if="!row.banedStatus"
                type="danger"
                size="mini"
                @click="onDelImage(row)"
              >删除图片</el-button>
            </template>
          </el-table-column>

        </el-table>
        <TablePagination
          :current-page="listQuery.page"
          :limit="listQuery.limit"
          :total="total"
          :loading="listLoading"
          @handleSizeChange="handleSizeChange"
          @handleCurrentChange="handleCurrentChangeOffset"
        />
      </div>
      <ImageViewer
        :visible.sync="showImage"
        :url="videoUrl"
        :type="'image'"
      />
    </el-tabs>
  </div>
</template>

<script>
import TablePagination from '@/components/TablePagination'
import ImageViewer from '@/components/ImageViewer'

import { xhrVideoThumbList, xhrVideoThumbDelete, xhrVideoThumbVerify } from '@api/anchor'
import AnchorMixin from './mixin'
export default {
  name: 'VideoThumbs',
  components: { TablePagination, ImageViewer },
  mixins: [AnchorMixin],
  data() {
    return {
      list: [],
      total: 0,
      gender: '女',
      listQuery: {
        page: 1,
        limit: 50,
        userId: null
      },
      daterangeValue: '',
      showImage: false,
      videoUrl: '',
      selectRows: []
    }
  },

  created() {
    this.doSearch()
    setInterval(() => {
      this.doSearch()
    }, 1000 * 60)
  },
  methods: {
    doSearch() {
      if (!this.validateParams(this.listQuery)) {
        return
      }
      xhrVideoThumbList(this.listQuery)
        .then(resp => {
          this.list = resp.data.list
          this.total = resp.data.count
          this.listLoading = false
        })
        .catch(err => {
          this.$message.error(err)
          this.listLoading = false
        })
    },
    zoomImage(url) {
      this.showImage = true
      this.videoUrl = url
    },
    onGoDetail(id) {
      this.$router.push('/anchor/video-thumb-all?id=' + id)
    },
    onClickUser(uid) {
      this.$router.push('/account/account-list?id=' + uid)
    },

    onGenderChange(g) {
      this.$forceUpdate()
    },

    onVerify(row) {
      if (row.valid === 1) {
        return
      }
      row.valid = 1
      xhrVideoThumbVerify({
        id: row.chatId
      }).then(resp => {
        this.$message.success(row.chatId + '已阅!')
        this.$forceUpdate()
      }).catch(e => {
        row.valid = 0
        this.$forceUpdate()
      })
    },

    onDelImage(row) {
      this.$confirm('确定删除' + row.chatId + '的图片?').then(() => {
        xhrVideoThumbDelete({
          id: row.chatId
        })
          .then(resp => {
            this.$message.success('操作成功')
            this.doSearch()
          })
          .catch(err => {
            this.$message.error(err)
          })
      })
    },
    handleClick(tab) {
      switch (tab.name) {
        case 'first':
          this.$router.push('/anchor/chat-record')
          break
      }
    }

  }
}
</script>
<style lang="scss" scoped>
.video-wrap {
  position: relative;
  video {
    width: 100%;
  }
  &::after {
    content: '';
    position: absolute;
    width: 100%;
    height: 100%;
    left: 0;
    top: 0;
  }
}
</style>
